package top.soulblack.purchasesale.controller.setting;

import com.alibaba.fastjson.JSON;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.soulblack.purchasesale.common.Result;
import top.soulblack.purchasesale.model.GoodsColor;
import top.soulblack.purchasesale.service.GoodsColorService;

import javax.servlet.http.HttpServletRequest;

import java.util.List;

import static top.soulblack.purchasesale.common.CodeMsg.*;

/**
 * @Author: 廉雪峰
 * @Date: 2019/5/13 20:21
 * @Version 1.0
 */
@Controller
@RequestMapping("/color")
public class GoodsColorController {

    @Autowired
    private GoodsColorService goodsColorService;


    @RequestMapping("/addColor")
    @ResponseBody
    public Result<Boolean> addCorlor(GoodsColor goodsColor) {
        // 判断是否存在当前颜色
        if (goodsColorService.existColor(goodsColor.getName())) {
            return Result.error(COLOR_EXIST);
        }
        // 判断排序数是否在1到100区间
        if (goodsColor.getSort() > 100 || goodsColor.getSort() < 1) {
            return Result.error(SORT_ERROR);
        }
        goodsColorService.addColor(goodsColor);
        return Result.success(true);
    }

    @RequestMapping("/deleteColor")
    @ResponseBody
    // 后期需要加上User权限验证，否则暴露接口有危险。
    public Result<Boolean> deleteColor(Integer colorId) {
        if (goodsColorService.selectColor(colorId)){
            return Result.error(COLOR_LIFE);
        }
        if (goodsColorService.deleteColor(colorId)) {
            return Result.success(true);
        }
        return Result.error(COLOR_DELETE);
    }
    @RequestMapping("/editColor")
    @ResponseBody
    public Result<Boolean> editColor(GoodsColor goodsColor) {
        System.out.println(goodsColor.getGroup());
        if (goodsColorService.updateColor(goodsColor)) {
            return Result.success(true);
        }
        return Result.error(COLOR_EDIT_ERROR);
    }

    @RequestMapping("/editGroup")
    @ResponseBody
    public Result<Boolean> editGroup(String newGroup, String oldGroup) {
        if (goodsColorService.updateGroup(newGroup, oldGroup)) {
            return Result.success(true);
        }
        return Result.error(GROUP_EDIT_ERROR);
    }

    @RequestMapping("/deleteGroup")
    @ResponseBody
    public Result<Boolean> editGroup(String oldGroup) {
        if (goodsColorService.deleteGroup(oldGroup)) {
            return Result.success(true);
        }
        return Result.error(GROUP_EDIT_ERROR);
    }

}
